﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using BLL;
using DTO;

namespace CRM.GUI.HeThong
{
    public partial class ucDownloadConfig : UserControl
    {
        public ucDownloadConfig()
        {            
            InitializeComponent();
            LoadSavedPath();
        }

        System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration
                    (System.Windows.Forms.Application.ExecutablePath);

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (brwDialog.ShowDialog() == DialogResult.OK)
            {
                string selectedPath = brwDialog.SelectedPath + "\\";
                txtPath.Text = selectedPath;                
            }
        }

        private void LoadSavedPath()
        {
            txtPath.Text = config.AppSettings.Settings["SaveCallFilePath"].Value;
            txtDownloadPath.Text = config.AppSettings.Settings["DownloadPath"].Value;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (config.AppSettings.Settings["SaveCallFilePath"].Value != txtPath.Text)
            {
                string message = "";
                string selectedPath = txtPath.Text + @"\";
                Administrators ad = new Administrators();
                Dictionary<string, string> con = new Dictionary<string, string>();
                con.Add("SaveCallFilePath", selectedPath);
                ad.SaveConfig(con, ref message);
                MessageBox.Show(message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (config.AppSettings.Settings["DownloadPath"].Value != txtDownloadPath.Text)
            {
                string message = "";
                string selectedPath = txtDownloadPath.Text;
                Administrators ad = new Administrators();
                Dictionary<string, string> con = new Dictionary<string, string>();
                con.Add("DownloadPath", selectedPath);
                ad.SaveConfig(con, ref message);
                MessageBox.Show(message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SharedData.DownloadPath = selectedPath;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            var parentform = this.ParentForm;
            parentform.Close();
        }
    }
}
